<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../Universal.css">
</head>
<body>
    
    <div id="cnblogs_post_body" class="blogpost-body">
        <h1>数组</h1>
    <h3>数组的需求</h3>
    <p>现在需要统计某公司员工的工资情况，例如计算平均工资、最高工资等。假设该公司有50名员工，用前面所学的知识完成，那么程序首先需要声明50个变量来分别记住每位员工的工资，这样做会显得很麻烦.</p>
    <h3>数组的概述</h3>
    <p>数组是指一组数据的集合，数组中的每个数据被称作元素。在数组中可以存放任意类型的元素，但同一个数组里存放的元素类型必须一致。</p>
    <h3>数组的定义</h3>
    <p>格式:</p>
    <div class="cnblogs_code">
    <pre>数据类型[] 数组名 = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> 数据类型[元素个数或数组长度];
    
    举例:</span><span style="color: rgba(0, 0, 255, 1)">int</span>[] x = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[100];</pre>
    </div>
    <p><span style="line-height: 1.5">要点说明</span></p>
    <ul>
    <li>数据类型: 数组中存储元素的数据类型</li>
    <li>&nbsp;[] 表示数组的意思</li>
    <li>变量名 自定义标识符</li>
    <li>new 创建容器关键字</li>
    <li>元素个数,就是数组中,可以存储多少个数据 (恒定, 定长)</li>
    <li>数组是一个容器: 存储到数组中的每个元素,都有自己的自动编号(索引(index), 下标, 角标)</li>
    <li>访问数组存储的元素,必须依赖于索引,&nbsp;<span class="cnblogs_code"> 数组名[索引]</span></li>
    <li class="_mce_tagged_br"><span style="font-family: 宋体">数组中最小的索引是</span>0<span style="font-family: 宋体">，最大的索引是“数组的长度</span><span style="font-family: Calibri">-1</span><span style="font-family: 宋体">”。</span></li>
    </ul>
    <p><span style="font-family: 宋体">在程序中可以通过</span>“数组名<span style="font-family: Calibri">.length</span><span style="font-family: 宋体">”的方式来获得数组的长度，即元素的个数。</span></p>
    <p class="16">接下来，通过一个案例来演示如何定义数组以及访问数组中的元素，如下所示。ArrayDemo01.java</p>
    <div class="cnblogs_code">
    <pre> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ArrayDemo01 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
                   </span><span style="color: rgba(0, 0, 255, 1)">int</span>[] arr; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 声明变量</span>
                   arr = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[3]; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建数组对象</span>
                   System.out.println("arr[0]=" + arr[0]); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 访问数组中的第一个元素</span>
                   System.out.println("arr[1]=" + arr[1]); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 访问数组中的第二个元素</span>
                   System.out.println("arr[2]=" + arr[2]); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 访问数组中的第三个元素</span>
                   System.out.println("数组的长度是：" + arr.length); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印数组长度</span>
    <span style="color: rgba(0, 0, 0, 1)">           }
    }</span></pre>
    </div>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702140516336-1098531363.png" alt=""></p>
    <p><span style="font-family: 宋体">如果在使用数组时，不想使用这些默认初始值，也可以显式地为这些元素赋值。接下来通过一个程序来学习如何为数组的元素赋值，如下所示。</span>ArrayDemo02.java</p>
    <div class="cnblogs_code">
    <pre> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ArrayDemo02 {
         </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
                    </span><span style="color: rgba(0, 0, 255, 1)">int</span>[] arr = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[4]; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义可以存储4个整数的数组</span>
                    arr[0] = 1; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 为第1个元素赋值1</span>
                    arr[1] = 2; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 为第2个元素赋值2
                    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 下面的代码是打印数组中每个元素的值</span>
                    System.out.println("arr[0]=" + arr[0<span style="color: rgba(0, 0, 0, 1)">]);
                    System.out.println(</span>"arr[1]=" + arr[1<span style="color: rgba(0, 0, 0, 1)">]);
                    System.out.println(</span>"arr[2]=" + arr[2<span style="color: rgba(0, 0, 0, 1)">]);
                    System.out.println(</span>"arr[3]=" + arr[3<span style="color: rgba(0, 0, 0, 1)">]);
                }
    }</span></pre>
    </div>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702140700743-1600859845.png" alt=""></p>
    <p>在定义数组时只指定数组的长度，由系统自动为元素赋初值的方式称作动态初始化。</p>
    <p>在初始化数组时还有一种方式叫做静态初始化，就是在定义数组的同时就为数组的每个元素赋值。数组的静态初始化有两种方式，具体格式如下：</p>
    <div class="cnblogs_code">
    <pre>方式1  　　类型[] 数组名 = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> 类型[]{元素，元素，……};
    方式2 　　 类型[] 数组名 </span>= {元素，元素，元素，……};</pre>
    </div>
    <p><span style="font-family: 宋体">上面的两种方式都可以实现数组的静态初始化，但是为了简便，建议采用第二种方式。接下来通过一段代码来演示数组静态初始化的效果，如下所示。</span>ArrayDemo03.java</p>
    <div class="cnblogs_code">
    <pre> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ArrayDemo03 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
                    </span><span style="color: rgba(0, 0, 255, 1)">int</span>[] arr = { 1, 2, 3, 4 }; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 静态初始化
                    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 下面的代码是依次访问数组中的元素</span>
                    System.out.println("arr[0] = " + arr[0<span style="color: rgba(0, 0, 0, 1)">]);
                    System.out.println(</span>"arr[1] = " + arr[1<span style="color: rgba(0, 0, 0, 1)">]);
                    System.out.println(</span>"arr[2] = " + arr[2<span style="color: rgba(0, 0, 0, 1)">]);
                    System.out.println(</span>"arr[3] = " + arr[3<span style="color: rgba(0, 0, 0, 1)">]);
                }
     }</span></pre>
    </div>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702140924914-682488071.png" alt=""></p>
    <p>在操作数组时，经常需要依次访问数组中的每个元素，这种操作称作数组的遍历。接下来通过一个案例来学习如何使用for<span style="font-family: 宋体">循环来遍历数组，如下所示。</span>ArrayDemo04.java</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ArrayDemo04 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span>[] arr = { 1, 2, 3, 4, 5 }; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义数组
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 使用for循环遍历数组的元素</span>
            <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; arr.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
                System.out.println(arr[i]); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 通过索引访问元素</span>
    <span style="color: rgba(0, 0, 0, 1)">        }
        }
    }</span></pre>
    </div>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702141041539-1659043722.png" alt=""></p>
    <p>数组在编写程序时应用非常广泛，灵活地使用数组对实际开发很重要。接下来，本节将针对数组的常见操作进行详细地讲解，如数组的遍历、最值的获取、数组的排序等。</p>
    <p>在操作数组时，经常需要获取数组中元素的最值。接下来通过一个案例来演示如何获取数组中元素的最大值，如下所示。ArrayDemo05.java</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ArrayDemo05 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span>[] arr = { 4, 1, 6, 3, 9, 8 }; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义一个数组</span>
            <span style="color: rgba(0, 0, 255, 1)">int</span> max = arr[0]; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义变量max用于记住最大数，首先假设第一个元素为最大值
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 下面通过一个for循环遍历数组中的元素</span>
            <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> x = 1; x &lt; arr.length; x++<span style="color: rgba(0, 0, 0, 1)">) {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (arr[x] &gt; max) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 比较 arr[x]的值是否大于max</span>
                    max = arr[x]; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 条件成立，将arr[x]的值赋给max</span>
    <span style="color: rgba(0, 0, 0, 1)">            }
            }
            System.out.println(</span>"max=" + max); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印最大值</span>
    <span style="color: rgba(0, 0, 0, 1)">    }
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">max=9</span></pre>
    </div>
    <p>每个数组的索引都有一个范围，即0~length-1<span style="font-family: 宋体">。在访问数组的元素时，索引不能超出这个范围，否则程序会报错</span></p>
    <p><span style="font-family: 宋体">在使用变量引用一个数组时，变量必须指向一个有效的数组对象，如果该变量的值为</span>null<span style="font-family: 宋体">，则意味着没有指向任何数组，此时通过该变量访问数组的元素会出现空指针异常，接下来通过一个案例来演示这种异常，如下所示。</span>ArrayDemo07.java</p>
    <div class="cnblogs_code">
    <pre> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ArrayDemo07 {
         </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
                    </span><span style="color: rgba(0, 0, 255, 1)">int</span>[] arr = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[3]; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义一个长度为3的数组</span>
                    arr[0] = 5; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 为数组的第一个元素赋值</span>
                    System.out.println("arr[0]=" + arr[0]); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 访问数组的元素</span>
                    arr = <span style="color: rgba(0, 0, 255, 1)">null</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将变量arr置为null</span>
                    System.out.println("arr[0]=" + arr[0]); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 访问数组的元素</span>
    <span style="color: rgba(0, 0, 0, 1)">            }
    }</span></pre>
    </div>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702141339930-904244546.png" alt=""></p>
    <h2>二维数组</h2>
    <p>二维数组的定义有很多方式，接下来针对几种常见的方式进行详细地讲解，具体如下：</p>
    <p>第一种方式：&nbsp;<span class="cnblogs_code"><span style="color: rgba(0, 0, 255, 1)">int</span>[][] arr = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[3][4];</span>&nbsp;</p>
    <p>第二种方式：&nbsp;<span class="cnblogs_code"><span style="color: rgba(0, 0, 255, 1)">int</span>[][] arr = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[3][];</span>&nbsp;</p>
    <p>第三种方式：&nbsp;<span class="cnblogs_code"><span style="color: rgba(0, 0, 255, 1)">int</span>[][] arr = {{1,2},{3,4,5,6},{7,8,9}};</span>&nbsp;</p>
    <p>对二维数组中元素的访问也是通过角标的方式，如需访问二维数组中第一个元素数组的第二个元素，具体代码如下：</p>
    <div class="cnblogs_code">
    <pre>arr[0][1];</pre>
    </div>
    <p>操作二维数组时，经常需要获取数组中元素的值。接下来通过一个案例来演示如何获取数组中元素值，如下所示。ArrayDemo08.java</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ArrayDemo08 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args){
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义二维数组的方式</span>
            <span style="color: rgba(0, 0, 255, 1)">int</span>[][] arr = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[3][4<span style="color: rgba(0, 0, 0, 1)">];
    
            System.out.println( arr );
            System.out.println(</span>"二维数组的长度: " +<span style="color: rgba(0, 0, 0, 1)"> arr.length);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取二维数组的3个元素</span>
            System.out.println( arr[0<span style="color: rgba(0, 0, 0, 1)">] );
            System.out.println( arr[</span>1<span style="color: rgba(0, 0, 0, 1)">] );
            System.out.println( arr[</span>2<span style="color: rgba(0, 0, 0, 1)">] );
    
            System.out.println(</span>"打印第一个一维数组的元素值"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println( arr[</span>0][0<span style="color: rgba(0, 0, 0, 1)">] );
            System.out.println( arr[</span>0][1] );<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">访问的为二维数组中第1个一维数组的第2个元素</span>
            System.out.println( arr[0][2<span style="color: rgba(0, 0, 0, 1)">] );
            System.out.println( arr[</span>0][3<span style="color: rgba(0, 0, 0, 1)">] );
    
            System.out.println(</span>"打印第二个一维数组的元素值"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println( arr[</span>1][0<span style="color: rgba(0, 0, 0, 1)">] );
            System.out.println( arr[</span>1][1<span style="color: rgba(0, 0, 0, 1)">] );
            System.out.println( arr[</span>1][2<span style="color: rgba(0, 0, 0, 1)">] );
            System.out.println( arr[</span>1][3<span style="color: rgba(0, 0, 0, 1)">] );
    
            System.out.println(</span>"打印第三个一维数组的元素值"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println( arr[</span>2][0<span style="color: rgba(0, 0, 0, 1)">] );
            System.out.println( arr[</span>2][1<span style="color: rgba(0, 0, 0, 1)">] );
            System.out.println( arr[</span>2][2<span style="color: rgba(0, 0, 0, 1)">] );
            System.out.println( arr[</span>2][3<span style="color: rgba(0, 0, 0, 1)">] );
        }
    }</span></pre>
    </div>
    <p>运行结果：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 0, 1)">[[I@</span>1540e19d<span style="color: rgba(0, 0, 0, 1)">
    二维数组的长度: </span>3<span style="color: rgba(0, 0, 0, 1)">
    [I@677327b6
    [I@14ae5a5
    [I@7f31245a
    打印第一个一维数组的元素值
    </span>0
    0
    0
    0<span style="color: rgba(0, 0, 0, 1)">
    打印第二个一维数组的元素值
    </span>0
    0
    0
    0<span style="color: rgba(0, 0, 0, 1)">
    打印第三个一维数组的元素值
    </span>0
    0
    0
    0</pre>
    </div>
    <p>学习完了数组元素的访问，我们来学习下数组的遍历及数组的元素累加和操作。</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ArrayDemo09 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args){
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">一维数组的求累加和并遍历</span>
            <span style="color: rgba(0, 0, 255, 1)">int</span>[] arr = {10,20,30,40,50<span style="color: rgba(0, 0, 0, 1)">};
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> sum = 0<span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i=0; i&lt;arr.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.out.println(arr[i]);</span>
                sum +=<span style="color: rgba(0, 0, 0, 1)"> arr[i];
            }
            System.out.println(</span>"sum= " +<span style="color: rgba(0, 0, 0, 1)"> sum);
            System.out.println(</span>"---------------------"<span style="color: rgba(0, 0, 0, 1)">);
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">二维数组的求累加和并遍历</span>
            <span style="color: rgba(0, 0, 255, 1)">int</span>[][] arr2 = { {1,2},{3,4,5},{6,7,8,9,10<span style="color: rgba(0, 0, 0, 1)">} };
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> sum2 = 0<span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i=0; i&lt;arr2.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
                </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> j=0; j&lt;arr2[i].length; j++<span style="color: rgba(0, 0, 0, 1)">) {
                    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.out.println(arr2[i][j])</span>
                    sum2 +=<span style="color: rgba(0, 0, 0, 1)"> arr2[i][j];
                }
            }
            System.out.println(</span>"sum2= "+<span style="color: rgba(0, 0, 0, 1)"> sum2);
        }
    }</span></pre>
    </div>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702141843946-2081583668.png" alt=""></p>
    <p>接下来通过一个案例来熟悉二维数组的使用。例如要统计一个公司三个销售小组中每个小组的总销售额以及整个公司的销售额。如下所示</p>
    <ul>
    <li class="16"><span style="font-family: 宋体">第一小组销售额为</span>{11, 12}万元</li>
    <li class="16"><span style="font-family: 宋体">第二小组销售额为</span>{21, 22, 23}万元</li>
    <li class="16"><span style="font-family: 宋体">第三小组销售额为</span>{31, 32, 33, 34}万元。</li>
    </ul>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
     * Created by yang on 2017/7/2.
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ArrayDemo10 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span>[][] arr = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[3][]; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义一个长度为3的二维数组</span>
            arr[0] = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[]{11, 12}; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 为数组的元素赋值</span>
            arr[1] = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[]{21, 22, 23<span style="color: rgba(0, 0, 0, 1)">};
            arr[</span>2] = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[]{31, 32, 33, 34<span style="color: rgba(0, 0, 0, 1)">};
    
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> sum = 0; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义变量记录总销售额</span>
            <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; arr.length; i++) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 遍历数组元素</span>
                <span style="color: rgba(0, 0, 255, 1)">int</span> groupSum = 0; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义变量记录小组销售总额</span>
                <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> j = 0; j &lt; arr[i].length; j++) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 遍历小组内每个人的销售额</span>
                    groupSum = groupSum +<span style="color: rgba(0, 0, 0, 1)"> arr[i][j];
                }
                sum </span>= sum + groupSum; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 累加小组销售额</span>
                System.out.println("第" + (i + 1) + "小组销售额为：" + groupSum + " 万元"<span style="color: rgba(0, 0, 0, 1)">);
            }
            System.out.println(</span>"总销售额为: " + sum + " 万元"<span style="color: rgba(0, 0, 0, 1)">);
        }
    }</span></pre>
    </div>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702142106868-34781443.png" alt=""></p>
    <h3>JVM内存划分&nbsp;</h3>
    <p>&nbsp;JVM对自己的内存划分为5个区域</p>
    <ul>
    <li>寄存器:内存和CUP之间</li>
    <li>本地方法栈: JVM调用了系统中的功能</li>
    <li>方法和数据共享: 运行时期class文件进入的地方</li>
    <li>方法栈:所有的方法运行的时候进入内存</li>
    <li>堆:存储的是容器和对象</li>
    </ul>
    <h3>数组的内存</h3>
    <p>int[] x; // 声明一个int[]类型的变量<br>x = new int[100];		// 创建一个长度为100的数组<br>接下来，通过两张内存图来详细地说明数组在创建过程中内存的分配情况。<br>第一行代码 int[] x; 声明了一个变量x，该变量的类型为int[]，即一个int类型的数组。变量x会占用一块内存单元，它没有被分配初始值<br>第二行代码 x = new int[100]; 创建了一个数组，将数组的地址赋值给变量x。在程序运行期间可以使用变量x来引用数组，这时内存中的状态会发生变化</p>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702142732039-1063723182.png" alt=""><img style="line-height: 1.5" src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702142742836-849345736.png" alt=""></p>
    <h3><span style="line-height: 1.5">数组中常见的异常</span></h3>
    <p>数组操作中,常见的两个异常<br>    	 数组的索引越界异常<br>	 	 空指针异常</p>
    <h3><span style="line-height: 1.5">二维数组内存图</span></h3>
    <p>举例:int[][] arr = new int[3][4];<br>外层数组长在内存开辟连续的3个大的内存空间,每一个内存空间都对应的有地址值<br>每一个大内存空间里又开辟连续的四个小的内存空间.</p>
    <p>&nbsp;<img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702143034743-578977863.png" alt=""></p>
    <h1>方法</h1>
    <p><span style="font-family: 宋体">在</span>java<span style="font-family: 宋体">中，方法就是用来完成解决某件事情或实现某个功能的办法。</span></p>
    <h3><strong><span style="font-family: 华文楷体">方法的语法格式</span></strong></h3>
    <p><span style="font-family: 宋体">在</span>Java中，声明一个方法的具体语法格式如下：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 0, 1)">修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2,．．．．．．){ 
        执行语句
        ……… 
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> 返回值;
    }</span></pre>
    </div>
    <p>对于上面的语法格式中具体说明如下：</p>
    <ul>
    <li>修饰符：方法的修饰符比较多，有对访问权限进行限定的，有静态修饰符static，还有最终修饰符final等，这些修饰符在后面的学习过程中会逐步介绍</li>
    <li>返回值类型：用于限定方法返回值的数据类型</li>
    <li>参数类型：用于限定调用方法时传入参数的数据类型</li>
    <li>参数名：是一个变量，用于接收调用方法时传入的数据</li>
    <li>return关键字：用于结束方法以及返回方法指定类型的值</li>
    <li>返回值：被return语句返回的值，该值会返回给调用者</li>
    </ul>
    <h3><span style="font-size: 1.17em; line-height: 1.5">方法使用的注意事项</span></h3>
    <ul>
    <li><span style="font-family: 宋体">方法不调用，自己不执行</span></li>
    <li><span style="font-family: 宋体">方法中不能定义方法， 但是，方法中可以调用方法</span></li>
    <li><span style="font-family: 宋体">方法定义的位置在类中，其他方法的外面</span></li>
    <li><span style="font-family: 宋体">方法中的“参数类型&nbsp;参数名1，参数类型&nbsp;参数名2”被称作参数列表，它用于描述方法在被调用时需要接收的参数，如果方法不需要接收任何参数，则参数列表为空，即()内不写任何内容。</span></li>
    <li><span style="font-family: 宋体">如果方法没有明确的返回值类型，使用</span><span style="font-family: Calibri">'</span><span style="font-family: 宋体">空</span><span style="font-family: Calibri">'</span><span style="font-family: 宋体">类型， </span><span style="font-family: Calibri">void</span><span style="font-family: 宋体">表示</span></li>
    <li><span style="font-family: Calibri">void</span><span style="font-family: 宋体">只能在方法返回值类型位置使用，不能作为普通的数据类型使用</span></li>
    <li><span style="font-family: 宋体">如果方法返回值类型为</span><span style="font-family: Calibri">void</span><span style="font-family: 宋体">类型，可以省略 </span><span style="font-family: Calibri">return ;</span></li>
    </ul>
    <p class="16" align="justify">接下来通过一个案例来演示方法的定义与使用，如下图所示。MethodDemo01.java</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> MethodDemo01 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> area = getArea(3, 5); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 调用 getArea方法</span>
            System.out.println(" The area is " +<span style="color: rgba(0, 0, 0, 1)"> area);
        }
    
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 下面定义了一个求矩形面积的方法，接收两个参数，其中x为高，y为宽</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> getArea(<span style="color: rgba(0, 0, 255, 1)">int</span> x, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> y) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> temp = x * y; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 使用变量temp记住运算结果</span>
            <span style="color: rgba(0, 0, 255, 1)">return</span> temp; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将变量temp的值返回</span>
    <span style="color: rgba(0, 0, 0, 1)">    }
    }</span></pre>
    </div>
    <h3>&nbsp;方法的重载</h3>
    <p>Java允许在一个类中定义多个名称相同的方法，但是参数的类型或个数必须不同，这就是方法的重载。</p>
    <p>比如，下面的三个方法互为重载关系</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> add(<span style="color: rgba(0, 0, 255, 1)">int</span> x,<span style="color: rgba(0, 0, 255, 1)">int</span> y) {逻辑} <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">两个整数加法</span>
    
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> add(<span style="color: rgba(0, 0, 255, 1)">int</span> x,<span style="color: rgba(0, 0, 255, 1)">int</span> y,<span style="color: rgba(0, 0, 255, 1)">int</span> z) {逻辑} <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">三个整数加法</span>
    
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> add(<span style="color: rgba(0, 0, 255, 1)">double</span> x,<span style="color: rgba(0, 0, 255, 1)">double</span> y) {逻辑} <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">两个小数加法</span></pre>
    </div>
    <p>接下来演示方法重载的方式如下所示。MethodDemo03.java</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> MethodDemo03 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 下面是针对求和方法的调用</span>
            <span style="color: rgba(0, 0, 255, 1)">int</span> sum1 = add(1, 2<span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> sum2 = add(1, 2, 3<span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 0, 255, 1)">double</span> sum3 = add(1.2, 2.3<span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 下面的代码是打印求和的结果</span>
            System.out.println("sum1=" +<span style="color: rgba(0, 0, 0, 1)"> sum1);
            System.out.println(</span>"sum2=" +<span style="color: rgba(0, 0, 0, 1)"> sum2);
            System.out.println(</span>"sum3=" +<span style="color: rgba(0, 0, 0, 1)"> sum3);
        }
    
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 下面的方法实现了两个整数相加</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> add(<span style="color: rgba(0, 0, 255, 1)">int</span> x, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> y) {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> x +<span style="color: rgba(0, 0, 0, 1)"> y;
        }
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 下面的方法实现了三个整数相加</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> add(<span style="color: rgba(0, 0, 255, 1)">int</span> x, <span style="color: rgba(0, 0, 255, 1)">int</span> y, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> z) {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> x + y +<span style="color: rgba(0, 0, 0, 1)"> z;
        }
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 下面的方法实现了两个小数相加</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">double</span> add(<span style="color: rgba(0, 0, 255, 1)">double</span> x, <span style="color: rgba(0, 0, 255, 1)">double</span><span style="color: rgba(0, 0, 0, 1)"> y) {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> x +<span style="color: rgba(0, 0, 0, 1)"> y;
        }
    }</span></pre>
    </div>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702160449571-810856488.png" alt=""></p>
    <h3>重载的注意事项</h3>
    <p class="16">重载方法参数必须不同：</p>
    <ul>
    <li><span style="font-family: 宋体">参数个数不同，如</span>method(int x)<span style="font-family: 宋体">与</span><span style="font-family: Calibri">method(int x,int y)</span><span style="font-family: 宋体">不同</span></li>
    <li><span style="font-family: 宋体">参数类型不同，如</span>method(int x)<span style="font-family: 宋体">与</span><span style="font-family: Calibri">method(double x)</span><span style="font-family: 宋体">不同</span></li>
    <li><span style="font-family: 宋体">参数顺序不同，如</span>method(int x,double y)<span style="font-family: 宋体">与</span><span style="font-family: Calibri">method(double x,int y)</span><span style="font-family: 宋体">不同</span></li>
    </ul>
    <p class="16">重载只与方法名与参数类型相关，与返回值无关。<span style="font-family: 宋体">如</span>void method(int x)<span style="font-family: 宋体">与</span><span style="font-family: Calibri">int method(int y)</span><span style="font-family: 宋体">不是方法重载，不能同时存在</span></p>
    <p class="16">重载与具体的变量标识符无关。<span style="font-family: 宋体">如</span>method(int x)<span style="font-family: 宋体">与</span><span style="font-family: Calibri">method(int y)</span><span style="font-family: 宋体">不是方法重载，不能同时存在</span></p>
    <h3>&nbsp;<strong style="font-size: 1.17em; line-height: 1.5">参数传递</strong></h3>
    <p><span style="line-height: 1.5">定义方法时，参数列表中的变量，我们称为形式参数</span></p>
    <p class="16">调用方法时，传入给方法的数值，我们称为实际参数</p>
    <h3>参数传递图解与结论</h3>
    <p>&nbsp;<img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170702160802977-2042370146.png" alt=""></p>
    <p>通过上面的两段程序可以得出如下结论：</p>
    <ul>
    <li class="16">当调用方法时，如果传入的数值为基本数据类型（包含String类型），形式参数的改变对实际参数不影响</li>
    <li class="16">当调用方法时，<span style="font-family: 宋体">如果传入的数值为引用数据类型（</span>String类型除外），形式参数的改变对实际参数有影响</li>
    </ul>
    <h1>&nbsp;库存管理案例</h1>
    <p>现在，我们将原有的库存管理案例，进行业务逻辑的封装。</p>
    <p>将对下列功能进行方法封装：</p>
    <ul>
    <li class="16">打印库存清单功能</li>
    <li class="16">库存商品数量修改功能</li>
    <li class="16">退出程序功能</li>
    </ul>
    <p><span style="font-family: 宋体">编写代码，</span>StockMamager.<span style="font-family: Calibri">java</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.Scanner;
    
    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
     * Created by yang on 2017/7/2.
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> StockMamager {
        </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
         * 库存管理功能菜单
         * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> 管理员键盘输入的功能操作序号
         </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> chooseFunction() {
            System.out.println(</span>"-------------库存管理------------"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(</span>"1.查看库存清单"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(</span>"2.修改商品库存数量"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(</span>"3.退出"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(</span>"请输入要执行的操作序号："<span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">接收键盘输入的功能选项序号</span>
            Scanner sc = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Scanner(System.in);
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> choose =<span style="color: rgba(0, 0, 0, 1)"> sc.nextInt();
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> choose;
        }
        </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
         * 查看库存清单
         * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> brands 商品品牌型号
         * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> sizes 商品尺寸大小
         * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> prices 商品价格
         * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> counts 商品库存个数
         </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> printStore(String[] brands, <span style="color: rgba(0, 0, 255, 1)">double</span>[] sizes, <span style="color: rgba(0, 0, 255, 1)">double</span>[] prices, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">[] counts) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">统计总库存个数、统计库存总金额</span>
            <span style="color: rgba(0, 0, 255, 1)">int</span> totalCount = 0<span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">double</span> totalMoney = 0.0<span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; brands.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
                totalCount </span>+=<span style="color: rgba(0, 0, 0, 1)"> counts[i];
                totalMoney </span>+= counts[i] *<span style="color: rgba(0, 0, 0, 1)"> prices[i];
            }
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">列表顶部</span>
            System.out.println("---------------------------查看库存清单--------------------------"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(</span>"品牌型号        尺寸    价格    库存数"<span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">列表中部</span>
            <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; brands.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
                System.out.println(brands[i]</span>+"    "+sizes[i]+"    "+prices[i]+"    "+<span style="color: rgba(0, 0, 0, 1)">counts[i]);
            }
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">列表底部</span>
            System.out.println("-------------------------------------------------------------"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(</span>"总库存数："+<span style="color: rgba(0, 0, 0, 1)">totalCount);
            System.out.println(</span>"库存商品总金额："+<span style="color: rgba(0, 0, 0, 1)">totalMoney);
        }
    
        </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
         * 修改商品库存数量
         * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> brands 商品品牌型号
         * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> counts 商品库存个数
         </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> update(String[] brands, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">[] counts){
            System.out.println(</span>"------------修改商品库存数量-----------"<span style="color: rgba(0, 0, 0, 1)">);
            </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; brands.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
                System.out.println(</span>"请输入"+ brands[i] +"商品库存数"<span style="color: rgba(0, 0, 0, 1)">);
                counts[i] </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Scanner(System.in).nextInt();
            }
        }
    
        </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
         * 退出
         </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> exit(){
            System.out.println(</span>"----------------退出---------------"<span style="color: rgba(0, 0, 0, 1)">);
            System.out.println(</span>"您已退出系统"<span style="color: rgba(0, 0, 0, 1)">);
        }
    
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">记录库存商品信息
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">品牌型号</span>
            String[] brands = <span style="color: rgba(0, 0, 255, 1)">new</span> String[]{"MacBookAir", "ThinkpadT450"<span style="color: rgba(0, 0, 0, 1)">};
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">尺寸大小</span>
            <span style="color: rgba(0, 0, 255, 1)">double</span>[] sizes = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">double</span>[]{13.3, 14.0<span style="color: rgba(0, 0, 0, 1)">};
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">价格</span>
            <span style="color: rgba(0, 0, 255, 1)">double</span>[] prices = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">double</span>[]{6988.88, 5999.99<span style="color: rgba(0, 0, 0, 1)">};
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">库存个数</span>
            <span style="color: rgba(0, 0, 255, 1)">int</span>[] counts = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[]{0, 0<span style="color: rgba(0, 0, 0, 1)">};
    
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">通过while循环模拟管理员进行功能重复选择操作</span>
            <span style="color: rgba(0, 0, 255, 1)">while</span> (<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">) {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打印功能菜单操作,接收键盘输入的功能选项序号</span>
                <span style="color: rgba(0, 0, 255, 1)">int</span> choose =<span style="color: rgba(0, 0, 0, 1)"> chooseFunction();
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行序号对应的功能</span>
                <span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)"> (choose) {
                    </span><span style="color: rgba(0, 0, 255, 1)">case</span> 1:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查看库存清单</span>
    <span style="color: rgba(0, 0, 0, 1)">                    printStore(brands, sizes, prices, counts);
                        </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                    </span><span style="color: rgba(0, 0, 255, 1)">case</span> 2:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">修改商品库存数量</span>
    <span style="color: rgba(0, 0, 0, 1)">                    update(brands, counts);
                        </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                    </span><span style="color: rgba(0, 0, 255, 1)">case</span> 3:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">退出</span>
    <span style="color: rgba(0, 0, 0, 1)">                    exit();
                        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
                    </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">:
                        System.out.println(</span>"----------------------------------"<span style="color: rgba(0, 0, 0, 1)">);
                        System.out.println(</span>"功能选择有误，请输入正确的功能序号!"<span style="color: rgba(0, 0, 0, 1)">);
                        </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                }
            }
        }
    }</span></pre>
    </div>
    <p>&nbsp;运行结果如下：</p>
    <div class="cnblogs_code">
    <pre>-------------库存管理------------
    1<span style="color: rgba(0, 0, 0, 1)">.查看库存清单
    </span>2<span style="color: rgba(0, 0, 0, 1)">.修改商品库存数量
    </span>3<span style="color: rgba(0, 0, 0, 1)">.退出
    请输入要执行的操作序号：
    </span>1
    ---------------------------查看库存清单--------------------------<span style="color: rgba(0, 0, 0, 1)">
    品牌型号        尺寸    价格    库存数
    MacBookAir    </span>13.3    6988.88    0<span style="color: rgba(0, 0, 0, 1)">
    ThinkpadT450    </span>14.0    5999.99    0
    -------------------------------------------------------------<span style="color: rgba(0, 0, 0, 1)">
    总库存数：</span>0<span style="color: rgba(0, 0, 0, 1)">
    库存商品总金额：</span>0.0
    -------------库存管理------------
    1<span style="color: rgba(0, 0, 0, 1)">.查看库存清单
    </span>2<span style="color: rgba(0, 0, 0, 1)">.修改商品库存数量
    </span>3<span style="color: rgba(0, 0, 0, 1)">.退出
    请输入要执行的操作序号：
    </span>2
    ------------修改商品库存数量-----------<span style="color: rgba(0, 0, 0, 1)">
    请输入MacBookAir商品库存数
    </span>222<span style="color: rgba(0, 0, 0, 1)">
    请输入ThinkpadT450商品库存数
    </span>15454
    -------------库存管理------------
    1<span style="color: rgba(0, 0, 0, 1)">.查看库存清单
    </span>2<span style="color: rgba(0, 0, 0, 1)">.修改商品库存数量
    </span>3<span style="color: rgba(0, 0, 0, 1)">.退出
    请输入要执行的操作序号：
    </span>1
    ---------------------------查看库存清单--------------------------<span style="color: rgba(0, 0, 0, 1)">
    品牌型号        尺寸    价格    库存数
    MacBookAir    </span>13.3    6988.88    222<span style="color: rgba(0, 0, 0, 1)">
    ThinkpadT450    </span>14.0    5999.99    15454
    -------------------------------------------------------------<span style="color: rgba(0, 0, 0, 1)">
    总库存数：</span>15676<span style="color: rgba(0, 0, 0, 1)">
    库存商品总金额：</span>9.427537682E7
    -------------库存管理------------
    1<span style="color: rgba(0, 0, 0, 1)">.查看库存清单
    </span>2<span style="color: rgba(0, 0, 0, 1)">.修改商品库存数量
    </span>3<span style="color: rgba(0, 0, 0, 1)">.退出
    请输入要执行的操作序号：
    </span>3
    ----------------退出---------------<span style="color: rgba(0, 0, 0, 1)">
    您已退出系统
    
    Process finished with exit code </span>0</pre>
    </div>
    <p>&nbsp;</p>
    </div>

</body>
</html>